/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package src.view.saving;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import org.primefaces.context.RequestContext;
import src.domain.saving.Savtrxgl;
import src.domain.saving.Savwd;
import src.domain.security.Users;
import src.services.saving.SavtrxglModel;
import src.services.saving.SavwdModel;
import src.util.Constanta;
import src.util.Messager;
import src.view.security.LoginBean;
import src.view.system.ApprovalBean;

/**
 *
 * @author jay
 */
@ManagedBean
@SessionScoped
public class SavwdglapprovalBean implements Serializable {

    private Savwd savwd;  
    private List<Savtrxgl> availableGL;  
    private List<Savwd> unauthorizedSavwds;  

    private SavwdModel savhelper;
    private boolean editMode = false, authorized=false;
    private BigDecimal saldoAwal = BigDecimal.ZERO, saldoAkhir = BigDecimal.ZERO;
    private Users users;
    private ApprovalBean approvalBean;
    
    public SavwdglapprovalBean() {
        super();
        ambilUser();
        this.savhelper = new SavwdModel();
        this.unauthorizedSavwds = new ArrayList<>();
        FacesContext context = FacesContext.getCurrentInstance();
        approvalBean = (ApprovalBean) context.getApplication().evaluateExpressionGet(context, "#{approvalBean}", ApprovalBean.class);
        approvalBean.setUpdateTab(false);
    }

    public boolean isEditMode() {
        return editMode;
    }

    public boolean isAuthorized() {
        return authorized;
    }
    
    public Savwd getSavwd() {
        return savwd;
    }

    public void setSavwd(Savwd savwd) {
        this.savwd = savwd;
    }

    public List<Savwd> getUnauthorizedSavwds() {
        return unauthorizedSavwds;
    }

    public List<Savtrxgl> getAvailableGL() {
        return availableGL;
    }
        
    public BigDecimal getSaldoAkhir() {
        return saldoAkhir;
    }

    public BigDecimal getSaldoAwal() {
        return saldoAwal;
    }
    
    public void calcSaldoAhir() {
        if (savwd.getAmount() != null)
            this.saldoAwal = savwd.getSaving().getBalance();
            this.saldoAkhir = saldoAwal.subtract(savwd.getAmount());
    }
            
    public final void loadUnauthorizedsavwd() {
        Map<String, Object> savfilter = new HashMap<>();
        Map<Object, Object> amtfilter = new HashMap<>();
        amtfilter.put("<=", users.getUgroup().getSavMaxWd());
        savfilter.put("approvalUid", null);
        savfilter.put("savtrxglId", "notnull");
        savfilter.put("amount", amtfilter);
        
        this.unauthorizedSavwds = savhelper.findAll(savfilter, " order by transDate");
    }
        
    private void initForm() {
        this.editMode = true;
        this.authorized = true;
                
        Map filter = new HashMap<>();
        filter.put("transactionName", Constanta.SAV_GL_DROPDOWN);
        this.availableGL = new SavtrxglModel().findAll(filter);
        saldoAkhir= BigDecimal.ZERO;
        saldoAwal= BigDecimal.ZERO;
    }
    
    public void openData() {
        approvalBean.setUpdateTab(false);
        initForm();
        if (savwd != null) calcSaldoAhir();
    }
    
    public void approve() {
        RequestContext context = RequestContext.getCurrentInstance();
        
        //men-triger history and balance
        savwd.setPosted(true);
        
        savwd.setApprovalUid(users.getUserId());
        boolean success = savhelper.saveData(savwd);
        
        if (success) {
            this.unauthorizedSavwds.remove(this.savwd);
            this.savwd = null;
            approvalBean.updateTabOtor();
        } 
        
        Messager.showSaveUpdateStatus(success);     
        context.addCallbackParam("success", success);  
    }
    
    private void ambilUser() {
        FacesContext context = FacesContext.getCurrentInstance();
        LoginBean logBean = (LoginBean) context.getApplication().evaluateExpressionGet(context, "#{loginBean}", LoginBean.class);
        this.users = logBean.getSelectedUser();
    }
}

